package com.example.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.entity.Category;
import com.example.entity.Dish;
import com.example.service.CategoryService;
import com.example.service.DishService;
import com.example.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 菜品及套餐分类 前端控制器
 * </p>
 *
 * @author bd
 * @since 2023-05-27
 */
@RestController
@RequestMapping("/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;



    @GetMapping("/page")
    public R<Page> categoryList(int page, int pageSize)
    {
        Page<Category> pageInfo=new Page<>(page,pageSize);
        LambdaQueryWrapper<Category> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.orderByDesc(Category::getCreateTime);

        categoryService.page(pageInfo,lambdaQueryWrapper);
        return R.success(pageInfo);

    }

    //新增菜品分类
    @PostMapping
    public R<String> newaddcaiping(@RequestBody Category category)
    {
        categoryService.save(category);
        return R.success("添加成功");
    }


    @PutMapping
    public R<String> updateCategory(@RequestBody Category category)
    {
        categoryService.updateById(category);
        return R.success("修改成功");
    }

    @DeleteMapping()  // http://localhost:8080/category?ids=1663435029456175106   注意使用的？而不是restful风格的/ ，所以不用@PathVariable
    public R<String> delete(Long ids)
    {
        boolean b = categoryService.removeById(ids);
        if(b)
        return R.success("删除成功");
        else
            return R.success("删除失败");
    }


    //新增菜品  (菜品分类数据回显)
    @GetMapping("/list")
    public R<List<Category>> categorylist(int type)
    {
        LambdaQueryWrapper<Category> categoryLambdaQueryWrapper=new LambdaQueryWrapper<>();
        categoryLambdaQueryWrapper.eq(Category::getType,type);
        List<Category> list = categoryService.list(categoryLambdaQueryWrapper);
        return R.success(list);
    }
}
